package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.CommentFabulousAdapter;
import com.niji.littly_donkey_android.adapter.FansAdapter;
import com.niji.littly_donkey_android.bean.CommentFabulous;
import com.niji.littly_donkey_android.bean.Fans;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;

import java.util.*;

public class FaulousActivity extends BaseActivity {
    private static final String TAG = "FaulousActivity";

    private Button comment_fabulous_return;

    private ListView comment_fabulous_list;




    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comment_fabulous_activity);
        init();
        commentFabulousReturn();
        commentFabulousClick();
    }

    private void commentFabulousClick() {
        comment_fabulous_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView veiwId = view.findViewById(R.id.comment_fabulous_name);
                String text = veiwId.getText().toString() + position;
                Log.i(TAG,text);
                Toast.makeText(FaulousActivity.this,text,Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.comment_fabulous_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }
    private void init() {
        comment_fabulous_list = findViewById(R.id.comment_fabulous_list);
        ArrayList<CommentFabulous> commentFabulous = new ArrayList<>();
        String url = UrlUtil.URL + "/user/comments/log/list";
        RequestQueue requestQueue= Volley.newRequestQueue(FaulousActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) dataMap.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            CommentFabulous fabulous = new CommentFabulous();
                            Integer id = (Integer) mapList.get(i).get("id");
                            fabulous.setId(id);
                            Map<String, Object> userInfo = (Map<String, Object>) mapList.get(i).get("userInfo");
                            String nickname = (String) userInfo.get("nickname");
                            fabulous.setName(nickname);
                            String avatar = (String)userInfo.get("avatar");
                            fabulous.setHead(UrlUtil.URL + avatar);
                            String signature = (String) userInfo.get("signature");
                            fabulous.setMessage(signature);
                            Integer type = (Integer) mapList.get(i).get("type");
                            Integer status = (Integer) mapList.get(i).get("status");
                            Integer user_id = (Integer) mapList.get(i).get("user_id");
                            Integer comments_id = (Integer) mapList.get(i).get("comments_id");
                            String created_at = (String) mapList.get(i).get("created_at");
                            fabulous.setTime(created_at);
                            fabulous.setUserId(user_id);
                            fabulous.setCommentsId(comments_id);
                            fabulous.setStatus(status);
                            if (type == 1){
                                fabulous.setCf("");
                            }else if(type == 2){
                                fabulous.setCf("评论了你的作品");
                            }

                            commentFabulous.add(fabulous);

                        }

                    }else if(code == -1){
                        Toast.makeText(FaulousActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(FaulousActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(FaulousActivity.this, "评论点赞列表获取失败", Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                comment_fabulous_list.setAdapter(new CommentFabulousAdapter(FaulousActivity.this,commentFabulous));
                RelativeLayout comment_fabulous_loading = findViewById(R.id.comment_fabulous_loading);
                RelativeLayout comment_fabulous_main = findViewById(R.id.comment_fabulous_main);
                comment_fabulous_main.setVisibility(View.VISIBLE);
                comment_fabulous_loading.setVisibility(View.INVISIBLE);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(FaulousActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
    }


    private void commentFabulousReturn() {
        comment_fabulous_return = findViewById(R.id.comment_fabulous_return);
        comment_fabulous_return = findViewById(R.id.comment_fabulous_return);
        comment_fabulous_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });

    }
}
